package com.groupbuying.customer.service.impl;

import com.groupbuying.customer.connector.WechatAliFeign;
import com.groupbuying.customer.constants.CommonConstants;
import com.groupbuying.customer.domain.CustomerDO;
import com.groupbuying.customer.domain.CustomerUshopDO;
import com.groupbuying.customer.domain.CustomerWxinfoRelDO;
import com.groupbuying.customer.domain.UserToken;
import com.groupbuying.customer.service.CustomerService;
import com.groupbuying.customer.service.RedPacketRegisterAndLoginService;
import com.groupbuying.customer.utils.JwtUtils;
import com.groupbuying.customer.utils.apiresult.ApiResult;
import com.groupbuying.customer.utils.apiresult.ApiResultCode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: mayunfei
 * @Date: Create in 15:19 2019/12/19
 * @tar:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RedPacketRegisterAndLoginServiceImpl implements RedPacketRegisterAndLoginService {
    private Logger log = LoggerFactory.getLogger(RedPacketRegisterAndLoginServiceImpl.class);

    @Autowired
    private WechatAliFeign wechatAliFeign;
    @Autowired
    private CustomerService customerService;


//    @Override
//    public ApiResult unionidLogin(String encryptedData, String iv ,String code,String indicate) {
//        if (StringUtils.isEmpty(encryptedData) || StringUtils.isEmpty(iv) || StringUtils.isEmpty(code) ){
//            log.error("参数不能为空");
//            return ApiResult.paramIsNull();
//        }
//        Map decrypt = wechatAliFeign.Decrypt(encryptedData,iv,code,"youdianproductor");
//        String unionid = (String) decrypt.get("unionId");
//        if (StringUtils.isEmpty(unionid)){
//            log.error("获取unionid失败");
//            return ApiResult.paramIsNull();
//        }
//        CustomerDO customer = customerService.getByUuid(unionid);
//        if (customer != null){
//      //     String phone =  customer.getMobile();
//       //    if (!StringUtils.isEmpty(phone)){
//               //登录返回token
//               try {
//                   Map result = new HashMap();
//                   UserToken userToken = new UserToken(customer.getMobile(), customer.getUuid(), "");
//                   String token = JwtUtils.generateToken(userToken, CommonConstants.COMMON_TOKEN_DAY * 24 * 60 * 60 * 1000);
//                   result.put("token", token);
//                   result.put("mobile", customer.getMobile());
//                   log.info("登录成功");
//                   return ApiResult.success(ApiResultCode.SUCCESS_LOGIN, result);
//               } catch (Exception e) {
//                   log.error("登录失败");
//                   return ApiResult.failure(ApiResultCode.FAILURE);
//               }
//           }
//    //    }
//        return ApiResult.failure(ApiResultCode.FAILURE);
//    }

    @Override
    public ApiResult unionIdLogin(String wxCode) {
        //查询openId
        Map<String, String> map = wechatAliFeign.getOpenidAndSessionKeyByCode(wxCode, "youdianproductor");
        if (map == null) {
            log.info("map为空");
            return ApiResult.paramIsNull();
        }
        String unionId = map.get("unionid");
        if (unionId == null) {
            log.info("unionId为空");
            return ApiResult.failure();
        }
        //TODO 1、通过查询够样数据库用户的unionId 且存在手机号

        //TODO 2、可以让用户登录
        return null;
    }

    @Override
    public ApiResult authorizationUserInfoLogin(Map<String, Object> params) {
        String decryptedData = params.get("decryptedData")==null?null:params.get("decryptedData").toString();
        String iv = params.get("iv")==null?null:params.get("iv").toString();
        String code = params.get("code") ==null?null:params.get("code").toString();
        Map<String,String> wxMobileMsg = wechatAliFeign.decryptData(decryptedData,iv,code,"youdianproductor");
        log.info("微信授权信息解密信息:{}", wxMobileMsg);;
        if (wxMobileMsg == null || wxMobileMsg.size()<2) {
            return ApiResult.failure(ApiResultCode.AUTHORIZATIONFAILURE,"授权信息解密失败");
        }
        String unionId = wxMobileMsg.get("unionid");
        log.info("用户信息:{},unionid:{}","", unionId);
        //TODO 1、通过查询够样数据库用户的unionId 且存在手机号

        //TODO 2、可以让用户登录
        return null;
    }
}
